
#ifndef _AUTOLOCK_H__INCLUDED_
#define _AUTOLOCK_H__INCLUDED_

#include <memory>
#include "TemplateImpl/AutoLockImpl.h"
#include "Mutex.h"

namespace nebula { namespace utility {

class AutoLock
{
private:
	AutoLock(const AutoLock &); // not implemented
	AutoLock& operator= (const AutoLock &); // not implemented

	std::auto_ptr<auto_lock_impl_base> pImpl;
	bool unlocked;

public:
	template<typename lock_type>
	explicit AutoLock(lock_type* rhs) :
			pImpl(new auto_lock_impl<lock_type>(rhs)),
			unlocked(false)
	{
		pImpl->lock();
	}

	~AutoLock()
	{
		unlock();
	}

	inline void unlock()
	{
		if (!unlocked)
		{
			unlocked = true;
			pImpl->unlock();
		}
	}
};

}} // namespace nebula::utility

#endif
